package com.ebraga.trackerclub.android.exception;

import java.util.StringTokenizer;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class BackofficeException extends Exception {

	private static final long serialVersionUID = 1L;

	private static final Logger LOG = LogManager.getLogger(BackofficeException.class);

	public BackofficeException(Throwable e) {
		super(e);
		LOG.error(e.getMessage());
	}

	public BackofficeException(String msg) {
		super(msg);
	}

	public BackofficeException(String msg, Throwable e) {
		super(msg, e);
		LOG.error(e.getMessage());
	}

	public static String getClassName(Integer deep) {
		Throwable thr = new Throwable();
		thr.fillInStackTrace();
		StackTraceElement[] ste = thr.getStackTrace();

		return ste[deep].getClassName();
	}

	public static String getFileName(Integer deep) {
		Throwable thr = new Throwable();
		thr.fillInStackTrace();
		StackTraceElement[] ste = thr.getStackTrace();
		return ste[deep].getFileName();
	}

	public static String getMethodName(Integer deep) {
		Throwable thr = new Throwable();
		thr.fillInStackTrace();
		StackTraceElement[] ste = thr.getStackTrace();
		return ste[deep].getMethodName();
	}

	public static Integer getLineNumber(Integer deep) {
		Throwable thr = new Throwable();
		thr.fillInStackTrace();
		StackTraceElement[] ste = thr.getStackTrace();
		return ste[deep].getLineNumber();
	}

	public static String getReference(Integer deep) {
		Throwable thr = new Throwable();
		thr.fillInStackTrace();
		StackTraceElement[] ste = thr.getStackTrace();
		return ste[deep].toString();
	}

	public String getMessage() {
		String message = super.getMessage();
		if (message != null && message.trim().startsWith(";")) {
			StringTokenizer tokenizer = new StringTokenizer(message, ":");
			StringBuffer buffer = new StringBuffer();
			while (tokenizer.hasMoreTokens()) {
				buffer.setLength(0);
				buffer.append(tokenizer.nextToken());
			}
			return buffer.toString();
		} else if (message != null) {
			return message;
		}

		return null;
	}

	public BackofficeException(String msg, Long cod) {
		super(msg);
	}
	
	public BackofficeException( String msg , Throwable e , Long cod){  
        super(msg , e);
    } 

}
